package com.mybatisplugin.springboot.service;


import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.mybatisplugin.springboot.conf.redis.RedisKeys;
import com.mybatisplugin.springboot.mapper.UserMapper;
import com.mybatisplugin.springboot.model.Module;
import com.mybatisplugin.springboot.model.Role;
import com.mybatisplugin.springboot.model.User;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public Object getList(Map<String, Object> map) {
        return userMapper.getList(map);
    }
    
    public User UserName(String username) {
    	return userMapper.findByUserName(username);
    }

	public User findUserByUserName(String username) {
		Module module = new Module();
		module.setMname("1");
		Set<Module> modules = new HashSet<Module>();
		modules.add(module);
		
		Role role = new Role();
		role.setRname("2");
		role.setModules(modules);
		Set<Role> roles = new HashSet<Role>();
		roles.add(role);
		
		User user = new User();
		user.setRoles(roles);
		user.setUname("admin");
		user.setPassword("123456");
		return user;
	}
	
	public List<User> findByListIds(List<Integer> ids) {
		List<User> users = userMapper.findByListIds(ids);
		return users;
	}
	
	public void inserUser(User user) {
		userMapper.inserUser(user);
	}

	public List<User> findByArrayIds(Integer[] ids) {
		List<User> users = userMapper.findByArrayIds(ids);
		return users;
	}

	public List<User> findByMapIds(Map<String,Object> ids) {
		List<User> users = userMapper.findByMapIds(ids);
		return users;
	}
	
	@Cacheable(value = RedisKeys._CACHE_TEST)
    public String  testCache1(String id){
		System.out.println("进入了方法");
        return "zhaoliu";
    }
	
	/**
	 * 固定key
	 *
	 * @return
	 * @author SHANHY
	 * @create  2017年4月9日
	 */
	@Cacheable(value = RedisKeys._CACHE_TEST, key = "'" + RedisKeys._CACHE_TEST + "'")
	public String testCache() {
		System.out.println("testCache");
		return "";
	}
	
	/**
	 * 存储在Redis中的key自动生成，生成规则详见CachingConfig.keyGenerator()方法
	 *
	 * @param str1
	 * @param str2
	 * @return
	 * @author SHANHY
	 * @create  2017年4月9日
	 */
	@Cacheable(value = RedisKeys._CACHE_TEST)
	public String testCache2(String str1, String str2) {
		System.out.println("testCache2");
		return "";
	}
    
//    @CachePut(key="'user_:'+#id")
//    public void  testCache2(String id){
//        System.out.println("testCache");
//    }

}